home *** CD-ROM | disk | FTP | other *** search
- /* DumpLogMain.c */
- #define EXTERN /* */
- #include "DumpLog.h"
-
- #ifndef FALSE
- #define FALSE 0
- #define TRUE 1
- #endif
- #if MPW
- QDGlobals qd;
- #endif
-
- enum AppleMenu {
- kAppleAbout = 1
- };
- enum FileMenu {
- kFileQuit = 1
- };
- enum EditMenu {
- kEditUndo = 1,
- kEditUnused,
- kEditCut,
- kEditCopy,
- kEditPaste,
- kEditClear
- };
-
- void EventLoop(void);
- void InitializeMenus(void);
- void DoMouseEvent(void);
- void DoCommand(
- WindowPtr theWindow,
- long menuChoice
- );
- void AdjustMenus(void);
- void AdjustEditMenu(
- Boolean isDeskAcc
- );
- void MakeLogFileName(
- StringPtr logFileName
- );
- void CreateLogFile(
- ConstStr255Param logFileName
- );
- OSErr CreateOutputFile(
- OSType creator,
- ConstStr255Param fileName,
- short vRefNum,
- short *fileRefNum
- );
- void WriteOutputLine(
- ConstStr255Param datum,
- short fileRefNum
- );
- void WriteNewline(
- short fileRefNum
- );
- void CloseOutputFile(
- OSErr status,
- short fileRefNum,
- short volumeRefNum,
- ConstStr255Param fileName
- );
-
- WindowPtr MakeStatusWindow(void);
- void UpdateStatusWindow(
- WindowPtr statusWindowPtr
- );
- void ShowStatusString(
- WindowPtr statusWindowPtr,
- ConstStr255Param theText
- );
- WindowPtr gStatusWindow;
- #define IsOurWindow(theWindow) ((theWindow) == gStatusWindow)
- Str255 gLogFileName;
-
- #if TESTING
- LogRecordPtr gLogRecordPtr;
- #endif
-
- void
- main(void)
- {
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0);
- InitializeMenus();
- /* */
- MakeLogFileName(gLogFileName);
- CreateLogFile(gLogFileName);
- /* */
- gStatusWindow = MakeStatusWindow();
- if (gStatusWindow == NULL)
- DebugStr("\pDisplay window failed");
- else {
- SetWTitle(gStatusWindow, gLogFileName);
- if (1) { /* Debug: create a log using gLogFileName */
- LogRecordPtr testLogPtr;
-
- gLogFileName[gLogFileName[0]+1] = '\0';
- testLogPtr = MakeLogRecord((char *) &gLogFileName[1], 10);
- LogString(testLogPtr, 'Test', gLogFileName);
- }
- DoAllLogRecords();
- while (gQuitNow == FALSE)
- EventLoop();
- }
- CloseOutputFile(noErr, gFileRefNum, gVolRefNum, gFileName);
- }
-
- /*
- * Application event loop: process one event each time.
- */
- void
- EventLoop(void)
- {
- long menuChoice;
- register WindowPtr theWindow;
- GrafPtr savePort;
- Boolean isActivating;
- char theChar;
-
- if (gUpdateMenusNeeded)
- AdjustMenus();
- WaitNextEvent(
- everyEvent,
- &EVENT,
- (gInForeground) ? 6L : 60L,
- NULL
- );
- theWindow = FrontWindow();
- switch (EVENT.what) {
- case nullEvent:
- WriteAllLogRecords();
- break;
- case keyDown:
- case autoKey:
- if ((EVENT.message & charCodeMask) == '.'
- && (EVENT.modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((EVENT.modifiers & cmdKey) != 0) {
- if (EVENT.what == keyDown) {
- menuChoice = MenuKey(EVENT.message & charCodeMask);
- if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
- DoCommand(theWindow, menuChoice);
- else if (IsOurWindow(theWindow))
- goto doKeyDown;
- else {
- SysBeep(10);
- }
- }
- }
- else if (IsOurWindow(theWindow)) {
- doKeyDown: theChar = EVENT.message & charCodeMask;
- if (theChar != '\015')
- gMessageString[++gMessageString[0]] = theChar;
- else {
- ShowStatusString(gStatusWindow, gMessageString);
- }
- }
- else {
- SysBeep(10);
- }
- break;
- case mouseDown:
- DoMouseEvent();
- break;
- case updateEvt:
- theWindow = (WindowPtr) EVENT.message;
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- if (IsOurWindow(theWindow))
- UpdateStatusWindow(theWindow);
- else {
- DrawControls(theWindow);
- DrawGrowIcon(theWindow);
- }
- EndUpdate(theWindow);
- SetPort(savePort);
- break;
- case activateEvt:
- theWindow = (WindowPtr) EVENT.message;
- isActivating = ((EVENT.modifiers & activeFlag) != 0);
- goto activateEvent;
- break;
- case osEvt:
- switch (((unsigned long) EVENT.message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivating = ((EVENT.message & 0x01) != 0);
- activateEvent: if (isActivating) {
- /*
- * Activate this window. Activate events define theWindow from
- * the event record, while suspend/resume uses the pre-set
- * FrontWindow value.
- */
- SelectWindow(theWindow);
- (void) TEFromScrap();
- }
- if (IsOurWindow(theWindow)) {
- /*
- * Nothing special
- */
- }
- else {
- /* Desk accessory or what? */
- }
- gInForeground = isActivating;
- gUpdateMenusNeeded = TRUE;
- break;
- }
- break;
- }
- }
-
- /*
- * DoMouseEvent
- * The user clicked on something. Handle application-wide processing here, or call
- * a Test function for specific action.
- */
- void
- DoMouseEvent(void)
- {
- WindowPtr theWindow;
- short whichPart;
-
- whichPart = FindWindow(EVENT.where, &theWindow);
- if (theWindow == NULL)
- theWindow = FrontWindow();
- if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
- theWindow = FrontWindow();
- switch (whichPart) {
- case inDesk:
- break;
- case inMenuBar:
- InitCursor();
- DoCommand(theWindow, MenuSelect(EVENT.where));
- break;
- case inDrag:
- DragWindow(theWindow, EVENT.where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, EVENT.where)) {
- if (IsOurWindow(theWindow))
- gQuitNow = TRUE;
- else {
- SysBeep(10);
- }
- }
- break;
- case inContent:
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else if (IsOurWindow(theWindow)) {
- /* Nothing happens here */;
- }
- else {
- /* Nothing happens here */
- }
- break;
- default:
- break; /* Bogus click: ignore */
- }
- }
-
- /*
- * DoCommand
- * Process a menu or keystroke command.
- */
- void
- DoCommand(
- WindowPtr theWindow,
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem == kAppleAbout)
- ; /* Nothing happens here */
- else {
- GetMenuItemText(gAppleMenu, menuItem, menuText);
- AdjustEditMenu(TRUE);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
- }
- break;
- case MENU_File:
- switch (menuItem) {
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- }
- break;
- case MENU_Edit:
- if (SystemEdit(menuItem - 1) == FALSE)
- SysBeep(10);
- break;
- }
- HiliteMenu(0);
- }
-
- /*
- * AdjustMenus
- * Enable/disable menu options.
- */
- void
- AdjustMenus(void)
- {
- EnableItem(gFileMenu, kFileQuit);
- AdjustEditMenu(IsOurWindow(FrontWindow()) == FALSE);
- }
-
- /*
- * AdjustEditMenu
- * Enable/disable Edit Menu options.
- */
- void
- AdjustEditMenu(
- Boolean isDeskAcc
- )
- {
- if (isDeskAcc) {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
- else {
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- }
- }
-
- void
- InitializeMenus(void)
- {
- gAppleMenu = NewMenu(MENU_Apple, "\p\024");
- AppendMenu(gAppleMenu, "\p(No About;(-");
- AppendResMenu(gAppleMenu, 'DRVR');
- InsertMenu(gAppleMenu, 0);
- gFileMenu = NewMenu(MENU_File, "\pFile");
- AppendMenu(gFileMenu, "\pQuit/Q");
- InsertMenu(gFileMenu, 0);
- gEditMenu = NewMenu(MENU_Edit, "\pEdit");
- AppendMenu(gEditMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/P;Clear");
- InsertMenu(gEditMenu, 0);
- DrawMenuBar();
- InitCursor();
- }
-
-
- void
- DumpDrawLine(
- ConstStr255Param datum
- )
- {
- pstrcat(gCurrentLine, datum);
- WriteOutputLine(gCurrentLine, gFileRefNum);
- gCurrentLine[0] = 0;
- }
-
- void
- DumpDrawString(
- ConstStr255Param datum
- )
- {
- pstrcat(gCurrentLine, datum);
- }
-
- void
- DumpDrawText(
- const char *datum,
- unsigned short length
- )
- {
- short len;
-
- len = 255 - gCurrentLine[0];
- if (len > length)
- len = length;
- BlockMove(datum, &gCurrentLine[1] + gCurrentLine[0], len);
- gCurrentLine[0] += len;
- }
-
- void
- MakeLogFileName(
- StringPtr logFileName
- )
- {
- unsigned long now;
- Intl0Hndl dateFormat;
- Str255 timeString;
-
- GetDateTime(&now);
- dateFormat = (Intl0Hndl) GetIntlResource(1);
- (**dateFormat).dateOrder = ymd; /* Year month day */
- (**dateFormat).shrtDateFmt = (dayLdingZ | mntLdingZ | century);
- (**dateFormat).dateSep = '.';
- (**dateFormat).timeCycle = timeCycle24;
- (**dateFormat).timeFmt = (secLeadingZ | minLeadingZ | hrLeadingZ);
- (**dateFormat).timeSep = 0; /* No separator */
- IUDatePString(now, shortDate, logFileName, (Handle) dateFormat);
- pstrcat(logFileName, "\p ");
- IUTimePString(now, FALSE, timeString, (Handle) dateFormat);
- pstrcat(logFileName, timeString);
- }
-
- void
- CreateLogFile(
- ConstStr255Param logFileName
- )
- {
- OSErr status;
-
- status = GetVol(NULL, &gVolRefNum);
- if (status != noErr)
- gVolRefNum = 0;
- (void) CreateOutputFile(kOutputCreatorID, logFileName, gVolRefNum, &gFileRefNum);
- }
-
- OSErr
- CreateOutputFile(
- OSType creator,
- ConstStr255Param fileName,
- short vRefNum,
- short *fileRefNum
- )
- {
- OSErr status;
-
- /*
- * Create the file, elmininating any duplicate.
- */
- SetCursor(*GetCursor(watchCursor));
- BlockMove(fileName, gFileName, fileName[0] + 1);
- status = Create(fileName, vRefNum, creator, 'TEXT');
- if (status == dupFNErr) { /* Exists already? */
- status = FSDelete(fileName, vRefNum);
- if (status == noErr)
- status = Create(fileName, vRefNum, creator, 'TEXT');
- }
- if (status == noErr)
- status = FSOpen(fileName, vRefNum, fileRefNum);
- if (status != noErr)
- *fileRefNum = 0;
- InitCursor();
- return (status);
- }
-
-
- #define kEndOfLine 0x0D /* Return */
- static char gEndOfLine[1] = { kEndOfLine };
-
- /*
- * Write one line of text to the output file. Ignore errors.
- */
- void
- WriteOutputLine(
- ConstStr255Param datum,
- short fileRefNum
- )
- {
- long length;
-
- if (datum[0] > 0) {
- length = datum[0];
- (void) FSWrite(fileRefNum, &length, (Ptr) &datum[1]);
- #if 0
- {
- short i;
- Str255 work;
-
- /*
- * We need to hack the string to force 7-bit Ascii
- * for the Comm Toolbox windows.
- */
- BlockMove(datum, work, datum[0] + 1);
- for (i = 1; i <= work[0]; i++) {
- switch (work[i]) {
- case 0xCA: work[i] = ' '; break;
- case 0xA5: work[i] = '*'; break;
- case 0xD2: work[i] = '"'; break;
- case 0xD3: work[i] = '"'; break;
- case 0xD4: work[i] = '\''; break;
- case 0xD5: work[i] = '\''; break;
- default: break;
- }
- }
- ShowStatusString(gStatusWindow, work);
- }
- #else
- ShowStatusString(gStatusWindow, datum);
- #endif
- }
- WriteNewline(fileRefNum);
- }
-
- void
- WriteNewline(
- short fileRefNum
- )
- {
- long length;
-
- length = 1;
- (void) FSWrite(fileRefNum, &length, (Ptr) gEndOfLine);
- }
-
- void
- CloseOutputFile(
- OSErr status,
- short fileRefNum,
- short volumeRefNum,
- ConstStr255Param fileName
- )
- {
- (void) FSClose(fileRefNum);
- (void) FlushVol(NULL, volumeRefNum);
- if (status != noErr)
- (void) FSDelete(fileName, volumeRefNum);
- }
-
-
-
-